library(ROCR)
library(dbConnect)
library(odbc)
library(DBI)
library(rpart.plot)
library(rpart)
library(party)
library(tree)
library(gplots)
library(ineq)

#Estraggo tabella 17_18
con <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1")#connetto al DB
#query17 <- dbSendQuery(con, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1718 ") 
query17 <- dbSendQuery(con, "select * from AGGREGATE.CART_DISCOUNT_SHOP_SALES_1718 ") 
DATA17 <- dbFetch(query17)
DATASET17 <- DATA17 [3:ncol(DATA17)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
DATASET17
#Partizione dei dati
pd17 <- sample(2,nrow(DATASET17),replace=TRUE, prob= c(0.75,0.25))
TRAININGSet17 <- DATASET17[pd17==1,]
TESTSet17 <- DATASET17[pd17==2,]
#CART
#CART17 <- rpart(FlagOpen ~., data=TRAININGSet17, method="class")
CART17 <- rpart(FlagOpen ~ DISCOUNT+ GAIN   + MARGIN+ nRECEIPT, data=TRAININGSet17, method="class") 
CART17  
#Plot CART
boxcols <- c("red", "palegreen3")[CART17$frame$yval] #Color CART
rpart.plot(CART17 ,type=3, digits=3, fallen.leaves=TRUE,box.col = boxcols, uniform=TRUE )   #Plot CART
prp(CART17, box.col = boxcols, extra = 102, uniform=TRUE ) #PRETTY Plot CART
#Statistics
summary(CART17)
printcp(CART17)
#predizione Valori
p1 <- predict(CART17, TESTSet17, type="class")
p1
summary(p1)
#Gini index (two Methods)---> Misura l’impurità/disordine del dataset 
Gini(p1)
#Estraggo tabella 18_19
con2 <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1") 
#query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1819 ")
query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_DISCOUNT_SHOP_SALES_1819 ")
DATA18 <- dbFetch(query18)
#DATA18[is.na(DATA18)] <- 0        #sostituisco i Null con 0
#DATA18[DATA18 < 0] <- 0           #sostituisco i valori negativi con 0
DATASET18 <- DATA18 [2:ncol(DATA18)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
#Inserisco la tabella nell'ALGORITMO CART
p2 <- predict(CART17, DATASET18, type="class")
p2
Gini(p2)
summary(p2)
FINAL <- cbind(DATA18, p2)
FINAL





CART17 <- rpart(FlagOpen ~., data=DATASET18, method="class") 
CART17 
boxcols <- c("red", "palegreen3")[CART17$frame$yval] #Color CART
rpart.plot(CART17 ,type=3, digits=3, fallen.leaves=TRUE,box.col = boxcols, uniform=TRUE )   #Plot CART
prp(CART17, box.col = boxcols, extra = 102, uniform=TRUE ) #PRETTY Plot CART
# Prune (potare) the tree using the best cp.
bestcp <- CART17$cptable[which.min(CART17$cptable[,"xerror"]),"CP"]
ptree <- prune(CART17, cp = bestcp)


library(ROCR)
library(dbConnect)
library(odbc)
library(DBI)
library(rpart.plot)
library(rpart)
library(party)
library(tree)
library(gplots)
library(ineq)


#Estraggo tabella 17_18
con <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1")#connetto al DB
#query17 <- dbSendQuery(con, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1718 ") 
query17 <- dbSendQuery(con, "select * from AGGREGATE.CART_DISCOUNT_SHOP_SALES_1718 ") 
DATA17 <- dbFetch(query17)
DATASET17 <- DATA17 [3:ncol(DATA17)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
DATASET17
#Partizione dei dati
folds <- cut(seq(1,nrow(DATASET17)),breaks=70,labels=FALSE)
for(i in 1:70){
  #Segement your data by fold using the which() function 
  testIndexes <- which(folds==i,arr.ind=TRUE)
  TRAININGSet <- DATASET17[testIndexes, ]
  TESTSet <- DATASET17[-testIndexes, ]
  #Use the test and train data partitions however you desire...
}

#pd <- sample(2,nrow(DATASET17),replace=TRUE, prob= c(0.75,0.25))
#TRAININGSet <- DATASET17[pd==1,]
#TESTSet <- DATASET17[pd==2,]

#CART
#CART17 <- rpart(FlagOpen ~., data=TRAININGSet, method="class")
CART17 <- rpart(FlagOpen ~ nRECEIPT + DISCOUNT + MARGIN , data=TRAININGSet, method="class") 
CART17  
#Plot CART
boxcols <- c("red", "palegreen3")[CART17$frame$yval] #Color CART
rpart.plot(CART17 ,type=3, digits=3, fallen.leaves=TRUE,box.col = boxcols, uniform=TRUE )   #Plot CART
prp(CART17, box.col = boxcols, extra = 102, uniform=TRUE ) #PRETTY Plot CART
#Statistics
summary(CART17)
printcp(CART17)
plotcp(CART17)
#predizione Valori
p1 <- predict(CART17, TESTSet, type="class")
p1
summary(p1)
#Gini index (two Methods)---> Misura l’impurità/disordine del dataset 
Gini(p1)
#Estraggo tabella 18_19
con2 <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1") 
#query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1819 ")
query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_DISCOUNT_SHOP_SALES_1819 ")
DATA18 <- dbFetch(query18)
#DATA18[is.na(DATA18)] <- 0        #sostituisco i Null con 0
#DATA18[DATA18 < 0] <- 0           #sostituisco i valori negativi con 0
DATASET18 <- DATA18 [2:ncol(DATA18)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
#Inserisco la tabella nell'ALGORITMO CART
p2 <- predict(CART17, DATASET18, type="class")
p2
Gini(p2)
summary(p2)
FINAL <- cbind(DATA18, p2)
FINAL

----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------
									FINE
--------------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------
#ERROR
R_error <- 1.0 - (CM[1,]+CM[2,])/sum(CM)
mean(R_error)
#misClassification error  Training set data
tab <- table(p1,TrainingSet$FlagOpen)
print(tab)
1-sum(diag(tab))/sum(tab)

p1
pred=prediction(p1[,2],DATASET$FlagOpen)
perf=performance(pred,"tpr","fpr")
plot(perf)


#Confusion Matrix
CM <- table(TESTSet17$FlagOpen,p1)
CM
#ERROR
MAE <- function(actual,predicted) {mean(abs(actual - predicted))}
MAE(DATASET$FlagOpen,p1)

dbWriteTable(con, "DBSCAN_R3", DATA, overwrite=TRUE)





 
 
 #------------------------------OUTPUT-------------------------------------------------
 

> library(ROCR)
> library(dbConnect)
> library(odbc)
> library(DBI)
> library(rpart.plot)
> library(rpart)
> library(party)
> library(tree)
> library(gplots)
> library(ineq)
> 
> #Estraggo tabella 17_18
> con <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1")#connetto al DB
> query17 <- dbSendQuery(con, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1718 ") 
> DATA17 <- dbFetch(query17)
> #DATA17[is.na(DATA17)] <- 0        #sostituisco i Null con 0
> #DATA17[DATA17 < 0] <- 0           #sostituisco i valori negativi con 0
> DATASET17 <- DATA17 [3:ncol(DATA17)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
> DATASET17
        SALES       COGS     MARGIN #RECEIPT               SHOP_CHANNEL FlagOpen
1   121615.67   42307.00   79308.67      761 "01 - Property Full Price"    Close
2   124644.20   58064.00   66580.20      773 "01 - Property Full Price"     OPEN
3  2010490.85 1245288.00  765202.85    11333     "02 - Property Outlet"    Close
4   210105.62   70709.00  139396.62     1344 "01 - Property Full Price"    Close
5   259061.91  116996.00  142065.91     1599 "01 - Property Full Price"     OPEN
6   119190.34   50936.00   68254.34      726 "01 - Property Full Price"     OPEN
7   127261.47   32609.00   94652.47      681 "01 - Property Full Price"    Close
8   789964.41  525994.12  263970.29     5104     "02 - Property Outlet"    Close
9   922359.77  617622.69  304737.08     5642     "02 - Property Outlet"     OPEN
10  107608.47   37135.00   70473.47      656 "01 - Property Full Price"    Close
11  116493.43   54410.00   62083.43      747 "01 - Property Full Price"     OPEN
12  123308.24   36951.00   86357.24      741 "01 - Property Full Price"    Close
13  154150.68   68629.00   85521.68     1019 "01 - Property Full Price"     OPEN
14   53914.21   16723.00   37191.21      324 "01 - Property Full Price"    Close
15   65491.45   29418.00   36073.45      411 "01 - Property Full Price"     OPEN
16  164834.92   49695.00  115139.92     1025 "01 - Property Full Price"    Close
17  183490.04   82107.00  101383.04     1105 "01 - Property Full Price"     OPEN
18  249364.22  110291.00  139073.22     1470 "01 - Property Full Price"     OPEN
19  250470.28   66755.00  183715.28     1406 "01 - Property Full Price"    Close
20   82318.58   27143.00   55175.58      543 "01 - Property Full Price"    Close
21   94308.89   43248.00   51060.89      605 "01 - Property Full Price"     OPEN
22  340347.73  100975.00  239372.73     1910 "01 - Property Full Price"    Close
23  404844.31  177333.00  227511.31     2341 "01 - Property Full Price"     OPEN
24  280872.21   89545.00  191327.21     1620 "01 - Property Full Price"    Close
25  293329.92  134050.00  159279.92     1763 "01 - Property Full Price"     OPEN
26   81279.65   32183.00   49096.65      530 "01 - Property Full Price"    Close
27  134358.52   64133.00   70225.52      703 "01 - Property Full Price"    Close
28  987920.03  644657.00  343263.03     6899     "02 - Property Outlet"    Close
29  843951.81  204717.00  639234.81     3854 "01 - Property Full Price"    Close
30 1067825.66  436660.00  631165.66     4743 "01 - Property Full Price"     OPEN
31   66287.24   22201.00   44086.24      443 "01 - Property Full Price"    Close
32   72337.77   33455.00   38882.77      509 "01 - Property Full Price"    Close
33 1566705.57  414001.00 1152704.57     6842 "01 - Property Full Price"    Close
34 1903474.85  789325.00 1114149.85     7977 "01 - Property Full Price"     OPEN
35 1056891.10  690656.15  366234.95     7080     "02 - Property Outlet"    Close
36 1212378.30  779035.00  433343.30     8053     "02 - Property Outlet"     OPEN
37 2580204.31  625309.00 1954895.31    12054 "01 - Property Full Price"    Close
38 3197188.38 1283028.00 1914160.38    14058 "01 - Property Full Price"     OPEN
39  175820.96   58006.00  117814.96     1113 "01 - Property Full Price"    Close
40  238164.89  109942.00  128222.89     1437 "01 - Property Full Price"     OPEN
41  906426.02  520986.19  385439.83     6312           "09 - Ecommerce"    Close
42  974106.83  644815.00  329291.83     7278           "09 - Ecommerce"     OPEN
43   32682.10   11298.00   21384.10      247 "01 - Property Full Price"    Close
44   49481.72   23690.00   25791.72      410 "01 - Property Full Price"    Close
45  669026.01  171544.00  497482.01     2962 "01 - Property Full Price"    Close
46  738672.08  313287.00  425385.08     3347 "01 - Property Full Price"     OPEN
47  923451.81  614845.00  308606.81     6123     "02 - Property Outlet"    Close
48 1005668.28  677129.00  328539.28     6448     "02 - Property Outlet"     OPEN
49  112645.15   40680.00   71965.15      726 "01 - Property Full Price"    Close
50  133640.13   62919.00   70721.13      973 "01 - Property Full Price"     OPEN
51  259836.64  181915.00   77921.64     2101     "02 - Property Outlet"     OPEN
52  298020.68  199669.85   98350.83     2425     "02 - Property Outlet"    Close
53    3437.72     794.00    2643.72       30 "01 - Property Full Price"     OPEN
54  118843.65   40603.00   78240.65      717 "01 - Property Full Price"    Close
55  982855.42  266979.00  715876.42     4691 "01 - Property Full Price"    Close
56 1097232.67  454385.00  642847.67     4968 "01 - Property Full Price"     OPEN
57  108110.50   66948.00   41162.50      867     "02 - Property Outlet"     OPEN
58  449103.27  307249.15  141854.12     3682     "02 - Property Outlet"    Close
59 1218844.76  794766.05  424078.71     8476     "02 - Property Outlet"    Close
60 2003528.55 1276176.20  727352.35    11198     "02 - Property Outlet"    Close
61  401288.81  122756.00  278532.81     2235 "01 - Property Full Price"    Close
62   16333.85    8626.49    7707.36       97           "09 - Ecommerce"    Close
> #Partizione dei dati
> pd17 <- sample(2,nrow(DATASET17),replace=TRUE, prob= c(0.75,0.25))
> TRAININGSet17 <- DATASET17[pd17==1,]
> TESTSet17 <- DATASET17[pd17==2,]
> #CART
> CART17 <- rpart(FlagOpen ~., data=TRAININGSet17, method="class") 
> CART17  
n= 48 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

 1) root 48 19 Close (0.60416667 0.39583333)  
   2) COGS< 52052.5 11  1 Close (0.90909091 0.09090909) *
   3) COGS>=52052.5 37 18 Close (0.51351351 0.48648649)  
     6) SALES>=270354.4 26  9 Close (0.65384615 0.34615385)  
      12) COGS< 644736 17  4 Close (0.76470588 0.23529412) *
      13) COGS>=644736 9  4 OPEN (0.44444444 0.55555556) *
     7) SALES< 270354.4 11  2 OPEN (0.18181818 0.81818182) *
> #Plot CART
> boxcols <- c("red", "palegreen3")[CART17$frame$yval] #Color CART
> rpart.plot(CART17 ,type=3, digits=3, fallen.leaves=TRUE,box.col = boxcols, uniform=TRUE )   #Plot CART
> prp(CART17, box.col = boxcols, extra = 102, uniform=TRUE ) #PRETTY Plot CART
> #Statistics
> summary(CART17)
Call:
rpart(formula = FlagOpen ~ ., data = TRAININGSet17, method = "class")
  n= 48 

          CP nsplit rel error   xerror      xstd
1 0.18421053      0 1.0000000 1.000000 0.1783206
2 0.05263158      2 0.6315789 1.105263 0.1808911
3 0.01000000      3 0.5789474 1.210526 0.1821628

Variable importance
        COGS        SALES     #RECEIPT       MARGIN SHOP_CHANNEL 
          28           26           26           19            1 

Node number 1: 48 observations,    complexity param=0.1842105
  predicted class=Close  expected loss=0.3958333  P(node) =1
    class counts:    29    19
   probabilities: 0.604 0.396 
  left son=2 (11 obs) right son=3 (37 obs)
  Primary splits:
      COGS         < 52052.5  to the left,  improve=2.653665000, (0 missing)
      #RECEIPT     < 744      to the left,  improve=2.210965000, (0 missing)
      MARGIN       < 336277.4 to the right, improve=0.728030300, (0 missing)
      SALES        < 123976.2 to the left,  improve=0.680555600, (0 missing)
      SHOP_CHANNEL splits as  RLR,          improve=0.004487179, (0 missing)
  Surrogate splits:
      SALES    < 123976.2 to the left,  agree=0.938, adj=0.727, (0 split)
      #RECEIPT < 744      to the left,  agree=0.938, adj=0.727, (0 split)
      MARGIN   < 58629.51 to the left,  agree=0.854, adj=0.364, (0 split)

Node number 2: 11 observations
  predicted class=Close  expected loss=0.09090909  P(node) =0.2291667
    class counts:    10     1
   probabilities: 0.909 0.091 

Node number 3: 37 observations,    complexity param=0.1842105
  predicted class=Close  expected loss=0.4864865  P(node) =0.7708333
    class counts:    19    18
   probabilities: 0.514 0.486 
  left son=6 (26 obs) right son=7 (11 obs)
  Primary splits:
      SALES        < 270354.4 to the right, improve=3.4445280, (0 missing)
      MARGIN       < 175303.6 to the right, improve=3.2044350, (0 missing)
      #RECEIPT     < 1609.5   to the right, improve=2.6938940, (0 missing)
      COGS         < 190792.4 to the right, improve=1.6217810, (0 missing)
      SHOP_CHANNEL splits as  RLR,          improve=0.4159737, (0 missing)
  Surrogate splits:
      MARGIN   < 150672.9 to the right, agree=0.973, adj=0.909, (0 split)
      #RECEIPT < 1609.5   to the right, agree=0.973, adj=0.909, (0 split)
      COGS     < 85826    to the right, agree=0.919, adj=0.727, (0 split)

Node number 6: 26 observations,    complexity param=0.05263158
  predicted class=Close  expected loss=0.3461538  P(node) =0.5416667
    class counts:    17     9
   probabilities: 0.654 0.346 
  left son=12 (17 obs) right son=13 (9 obs)
  Primary splits:
      COGS         < 644736   to the left,  improve=1.2071390, (0 missing)
      SALES        < 914392.9 to the left,  improve=0.6942308, (0 missing)
      #RECEIPT     < 7179     to the left,  improve=0.5470085, (0 missing)
      MARGIN       < 336277.4 to the right, improve=0.4480186, (0 missing)
      SHOP_CHANNEL splits as  RLR,          improve=0.2055944, (0 missing)
  Surrogate splits:
      #RECEIPT     < 6989.5   to the left,  agree=0.923, adj=0.778, (0 split)
      SALES        < 996794.2 to the left,  agree=0.846, adj=0.556, (0 split)
      MARGIN       < 721614.4 to the left,  agree=0.731, adj=0.222, (0 split)
      SHOP_CHANNEL splits as  LRL,          agree=0.692, adj=0.111, (0 split)

Node number 7: 11 observations
  predicted class=OPEN   expected loss=0.1818182  P(node) =0.2291667
    class counts:     2     9
   probabilities: 0.182 0.818 

Node number 12: 17 observations
  predicted class=Close  expected loss=0.2352941  P(node) =0.3541667
    class counts:    13     4
   probabilities: 0.765 0.235 

Node number 13: 9 observations
  predicted class=OPEN   expected loss=0.4444444  P(node) =0.1875
    class counts:     4     5
   probabilities: 0.444 0.556 

> printcp(CART17)

Classification tree:
rpart(formula = FlagOpen ~ ., data = TRAININGSet17, method = "class")

Variables actually used in tree construction:
[1] COGS  SALES

Root node error: 19/48 = 0.39583

n= 48 

        CP nsplit rel error xerror    xstd
1 0.184211      0   1.00000 1.0000 0.17832
2 0.052632      2   0.63158 1.1053 0.18089
3 0.010000      3   0.57895 1.2105 0.18216
> #predizione Valori
> p1 <- predict(CART17, TESTSet17, type="class")
> p1
    4     6     7    18    19    21    26    32    44    46    53    56    58    62 
 OPEN Close Close  OPEN  OPEN Close Close Close Close Close Close Close Close Close 
Levels: Close OPEN
> summary(p1)
Close  OPEN 
   11     3 
> #Gini index (two Methods)---> Misura l’impurità/disordine del dataset 
> Gini(p1)
[1] 0.1386555
> #Confusion Matrix
> CM <- table(TESTSet17$FlagOpen,p1)
> CM
       p1
        Close OPEN
  Close     6    2
  OPEN      5    1
> #Estraggo tabella 18_19
> con2 <- dbConnect(odbc::odbc(),DRIVER="SQL Server", SERVER="192.168.2.14", DATABASE = "FASHION_RETAIL", UID="sa", PWD="Mediamente1") 
> #query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_SHOP_SALES_1819 ")
> query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_TYPE_SHOP_SALES_1819 ")
> #query18 <- dbSendQuery(con2, "select * from AGGREGATE.CART_19 ")
> DATA18 <- dbFetch(query18)
> #DATA18[is.na(DATA18)] <- 0        #sostituisco i Null con 0
> #DATA18[DATA18 < 0] <- 0           #sostituisco i valori negativi con 0
> DATASET18 <- DATA18 [3:ncol(DATA18)]      #elimino colonne 1,2 riferite ai negozi (SHOP_ID & SHOP)
> DATASET18
        SALES       COGS     MARGIN #RECEIPT               SHOP_CHANNEL FlagOpen
1   106553.33   51537.19   55016.14      747 "01 - Property Full Price"     OPEN
2   295459.63  118364.49  177095.14     1928 "01 - Property Full Price"     OPEN
3   184143.29   85919.29   98224.00     1202 "01 - Property Full Price"     OPEN
4   546175.95  213951.81  332224.14     3449 "01 - Property Full Price"     OPEN
5    92906.29   43812.22   49094.07      555 "01 - Property Full Price"     OPEN
6   399592.83  154281.32  245311.51     2222 "01 - Property Full Price"     OPEN
7   718046.23  529831.44  188214.79     5181     "02 - Property Outlet"     OPEN
8  2013599.22 1396745.03  616854.19    13739     "02 - Property Outlet"     OPEN
9   100081.54   48736.01   51345.53      647 "01 - Property Full Price"     OPEN
10  273609.82  110011.09  163598.73     1629 "01 - Property Full Price"     OPEN
11  124766.79   58762.69   66004.10      797 "01 - Property Full Price"     OPEN
12  378146.08  148094.49  230051.59     2313 "01 - Property Full Price"     OPEN
13   44618.86   22365.10   22253.76      307 "01 - Property Full Price"     OPEN
14  160533.96   66543.36   93990.60      976 "01 - Property Full Price"     OPEN
15  134574.01   62316.82   72257.19      900 "01 - Property Full Price"     OPEN
16  373236.96  141865.22  231371.74     2463 "01 - Property Full Price"     OPEN
17  241341.29  114716.18  126625.11     1423 "01 - Property Full Price"     OPEN
18  633005.56  232904.88  400100.68     3480 "01 - Property Full Price"     OPEN
19   80955.00   40390.78   40564.22      541 "01 - Property Full Price"     OPEN
20  222677.38   90879.88  131797.50     1434 "01 - Property Full Price"     OPEN
21  280858.15  133950.41  146907.74     1643 "01 - Property Full Price"     OPEN
22  863404.29  328956.40  534447.89     4983 "01 - Property Full Price"     OPEN
23  233024.56  110201.57  122822.99     1414 "01 - Property Full Price"     OPEN
24  731247.45  287184.45  444063.00     4315 "01 - Property Full Price"     OPEN
25  120187.01   51258.28   68928.73      801 "01 - Property Full Price"    Close
26  695720.72  307601.81  388118.91     3369 "01 - Property Full Price"     OPEN
27 2043890.84  716441.37 1327449.47     9735 "01 - Property Full Price"     OPEN
28   10211.89    6033.85    4178.04       88 "01 - Property Full Price"    Close
29  195678.85   80439.03  115239.82     1304 "01 - Property Full Price"    Close
30 1363526.47  601226.21  762300.26     6294 "01 - Property Full Price"     OPEN
31 3693148.09 1292886.46 2400261.63    16175 "01 - Property Full Price"     OPEN
32 1179751.44  840368.73  339382.71     8660     "02 - Property Outlet"     OPEN
33 2882611.34 1946116.92  936494.42    19926     "02 - Property Outlet"     OPEN
34 2042208.29  913477.02 1128731.27    10742 "01 - Property Full Price"     OPEN
35 6084006.39 2089021.65 3994984.74    29602 "01 - Property Full Price"     OPEN
36  147734.01   70612.78   77121.23      992 "01 - Property Full Price"     OPEN
37  441748.04  174159.36  267588.68     2813 "01 - Property Full Price"     OPEN
38 1522787.95  752220.79  770567.16    12388           "09 - Ecommerce"     OPEN
39 3556570.03 2188680.19 1367889.84    25642           "09 - Ecommerce"     OPEN
40   44142.53   16426.35   27716.18      339 "01 - Property Full Price"    Close
41  566074.90  206010.02  360064.88     2714 "01 - Property Full Price"     OPEN
42 1475297.34  491011.07  984286.27     6703 "01 - Property Full Price"     OPEN
43  942442.59  686984.00  255458.59     6866     "02 - Property Outlet"     OPEN
44 2356247.38 1643463.39  712783.99    16899     "02 - Property Outlet"     OPEN
45   81601.16   38683.51   42917.65      582 "01 - Property Full Price"     OPEN
46  288856.68  119643.04  169213.64     1896 "01 - Property Full Price"     OPEN
47  328199.44  245394.82   82804.62     2621     "02 - Property Outlet"     OPEN
48 1038007.47  726252.71  311754.76     7990     "02 - Property Outlet"     OPEN
49  101372.27   49419.10   51953.17      685 "01 - Property Full Price"     OPEN
50  300679.83  121053.01  179626.82     1888 "01 - Property Full Price"     OPEN
51 1016438.49  366506.74  649931.75     4878 "01 - Property Full Price"     OPEN
52 2515591.63  884320.01 1631271.62    11906 "01 - Property Full Price"     OPEN
53  527541.14  396824.40  130716.74     4745     "02 - Property Outlet"     OPEN
54 1322757.23  955939.72  366817.51    11488     "02 - Property Outlet"     OPEN
55 1172755.56  840966.22  331789.34     9014     "02 - Property Outlet"     OPEN
56 2990768.36 2061965.94  928802.42    21778     "02 - Property Outlet"     OPEN
57 2042843.11 1463925.13  578917.98    12415     "02 - Property Outlet"     OPEN
58 4812635.15 3224388.27 1588246.88    28783     "02 - Property Outlet"     OPEN
59  316817.30  142851.36  173965.94     1795 "01 - Property Full Price"     OPEN
60  935617.88  346216.39  589401.49     5300 "01 - Property Full Price"     OPEN
61   56131.37   34340.30   21791.07      394           "09 - Ecommerce"     OPEN
> #Inserisco la tabella nell'ALGORITMO CART
> p2 <- predict(CART17, DATASET18, type="class")
> p2
    1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18    19    20    21    22    23    24 
Close Close  OPEN Close Close Close Close  OPEN Close Close  OPEN Close Close  OPEN  OPEN Close  OPEN Close Close  OPEN Close Close  OPEN Close 
   25    26    27    28    29    30    31    32    33    34    35    36    37    38    39    40    41    42    43    44    45    46    47    48 
Close Close  OPEN Close  OPEN Close  OPEN  OPEN  OPEN  OPEN  OPEN  OPEN Close  OPEN  OPEN Close Close Close  OPEN  OPEN Close Close Close  OPEN 
   49    50    51    52    53    54    55    56    57    58    59    60    61 
Close Close Close  OPEN Close  OPEN  OPEN  OPEN  OPEN  OPEN Close Close Close 
Levels: Close OPEN
> Gini(p2)
[1] 0.1710134
> summary(p2)
Close  OPEN 
   34    27 
> FINAL <- cbind(DATA18, p2)
> FINAL
   SHOP_ID                                      SHOP      SALES       COGS     MARGIN #RECEIPT               SHOP_CHANNEL FlagOpen    p2
1    73539             "ITALY - BOLOGNA - Ugo Bassi"  106553.33   51537.19   55016.14      747 "01 - Property Full Price"     OPEN Close
2    73539             "ITALY - BOLOGNA - Ugo Bassi"  295459.63  118364.49  177095.14     1928 "01 - Property Full Price"     OPEN Close
3    73569          "ITALY - MILANO - C.so Vercelli"  184143.29   85919.29   98224.00     1202 "01 - Property Full Price"     OPEN  OPEN
4    73569          "ITALY - MILANO - C.so Vercelli"  546175.95  213951.81  332224.14     3449 "01 - Property Full Price"     OPEN Close
5    73638            "ITALY - SIENA - Via di Citt?"   92906.29   43812.22   49094.07      555 "01 - Property Full Price"     OPEN Close
6    73638            "ITALY - SIENA - Via di Citt?"  399592.83  154281.32  245311.51     2222 "01 - Property Full Price"     OPEN Close
7    73647   "ITALY - BARBERINO DI MUGELLO - Outlet"  718046.23  529831.44  188214.79     5181     "02 - Property Outlet"     OPEN Close
8    73647   "ITALY - BARBERINO DI MUGELLO - Outlet" 2013599.22 1396745.03  616854.19    13739     "02 - Property Outlet"     OPEN  OPEN
9    73716                         "ITALY - BRESCIA"  100081.54   48736.01   51345.53      647 "01 - Property Full Price"     OPEN Close
10   73716                         "ITALY - BRESCIA"  273609.82  110011.09  163598.73     1629 "01 - Property Full Price"     OPEN Close
11   73740         "ITALY - NAPOLI - Via Filangieri"  124766.79   58762.69   66004.10      797 "01 - Property Full Price"     OPEN  OPEN
12   73740         "ITALY - NAPOLI - Via Filangieri"  378146.08  148094.49  230051.59     2313 "01 - Property Full Price"     OPEN Close
13   73773                         "ITALY - PERUGIA"   44618.86   22365.10   22253.76      307 "01 - Property Full Price"     OPEN Close
14   73773                         "ITALY - PERUGIA"  160533.96   66543.36   93990.60      976 "01 - Property Full Price"     OPEN  OPEN
15   73801     "ITALY - BOLOGNA - Aeroporto G.Marco"  134574.01   62316.82   72257.19      900 "01 - Property Full Price"     OPEN  OPEN
16   73801     "ITALY - BOLOGNA - Aeroporto G.Marco"  373236.96  141865.22  231371.74     2463 "01 - Property Full Price"     OPEN Close
17   73825                "ITALY - BOLOGNA - Farini"  241341.29  114716.18  126625.11     1423 "01 - Property Full Price"     OPEN  OPEN
18   73825                "ITALY - BOLOGNA - Farini"  633005.56  232904.88  400100.68     3480 "01 - Property Full Price"     OPEN Close
19   73830                          "ITALY - VARESE"   80955.00   40390.78   40564.22      541 "01 - Property Full Price"     OPEN Close
20   73830                          "ITALY - VARESE"  222677.38   90879.88  131797.50     1434 "01 - Property Full Price"     OPEN  OPEN
21  175674              "ITALY - ROMA - Fiumicino C"  280858.15  133950.41  146907.74     1643 "01 - Property Full Price"     OPEN Close
22  175674              "ITALY - ROMA - Fiumicino C"  863404.29  328956.40  534447.89     4983 "01 - Property Full Price"     OPEN Close
23  876293              "ITALY - ROMA - Fiumicino B"  233024.56  110201.57  122822.99     1414 "01 - Property Full Price"     OPEN  OPEN
24  876293              "ITALY - ROMA - Fiumicino B"  731247.45  287184.45  444063.00     4315 "01 - Property Full Price"     OPEN Close
25 1103131                 "ITALY - **MILANO - Coin"  120187.01   51258.28   68928.73      801 "01 - Property Full Price"    Close Close
26 1489952     "ITALY - FIRENZE - P. della Signoria"  695720.72  307601.81  388118.91     3369 "01 - Property Full Price"     OPEN Close
27 1489952     "ITALY - FIRENZE - P. della Signoria" 2043890.84  716441.37 1327449.47     9735 "01 - Property Full Price"     OPEN  OPEN
28 2022028     "ITALY -** PALERMO - Via Libert? NEW"   10211.89    6033.85    4178.04       88 "01 - Property Full Price"    Close Close
29 2022028     "ITALY -** PALERMO - Via Libert? NEW"  195678.85   80439.03  115239.82     1304 "01 - Property Full Price"    Close  OPEN
30 2167235         "ITALY - ROMA - Piazza di Spagna" 1363526.47  601226.21  762300.26     6294 "01 - Property Full Price"     OPEN Close
31 2167235         "ITALY - ROMA - Piazza di Spagna" 3693148.09 1292886.46 2400261.63    16175 "01 - Property Full Price"     OPEN  OPEN
32 2203462       "ITALY - NOVENTA DI PIAVE - Outlet" 1179751.44  840368.73  339382.71     8660     "02 - Property Outlet"     OPEN  OPEN
33 2203462       "ITALY - NOVENTA DI PIAVE - Outlet" 2882611.34 1946116.92  936494.42    19926     "02 - Property Outlet"     OPEN  OPEN
34 2237727        "ITALY - MILANO - P.zza Duomo NEW" 2042208.29  913477.02 1128731.27    10742 "01 - Property Full Price"     OPEN  OPEN
35 2237727        "ITALY - MILANO - P.zza Duomo NEW" 6084006.39 2089021.65 3994984.74    29602 "01 - Property Full Price"     OPEN  OPEN
36 2264278       "ITALY - ROMA - Cola di Rienzo NEW"  147734.01   70612.78   77121.23      992 "01 - Property Full Price"     OPEN  OPEN
37 2264278       "ITALY - ROMA - Cola di Rienzo NEW"  441748.04  174159.36  267588.68     2813 "01 - Property Full Price"     OPEN Close
38 2265838                "EUR -  - ECommerce Shop " 1522787.95  752220.79  770567.16    12388           "09 - Ecommerce"     OPEN  OPEN
39 2265838                "EUR -  - ECommerce Shop " 3556570.03 2188680.19 1367889.84    25642           "09 - Ecommerce"     OPEN  OPEN
40 2267994                   "ITALY -** ROMA - Coin"   44142.53   16426.35   27716.18      339 "01 - Property Full Price"    Close Close
41 2286770   "ITALY - FERNO - MILANO  Malpensa DUTY"  566074.90  206010.02  360064.88     2714 "01 - Property Full Price"     OPEN Close
42 2286770   "ITALY - FERNO - MILANO  Malpensa DUTY" 1475297.34  491011.07  984286.27     6703 "01 - Property Full Price"     OPEN Close
43 2312294            "ITALY - FIDENZA - Outlet NEW"  942442.59  686984.00  255458.59     6866     "02 - Property Outlet"     OPEN  OPEN
44 2312294            "ITALY - FIDENZA - Outlet NEW" 2356247.38 1643463.39  712783.99    16899     "02 - Property Outlet"     OPEN  OPEN
45 2726114                  "ITALY - ROMA - Euroma2"   81601.16   38683.51   42917.65      582 "01 - Property Full Price"     OPEN Close
46 2726114                  "ITALY - ROMA - Euroma2"  288856.68  119643.04  169213.64     1896 "01 - Property Full Price"     OPEN Close
47 3145981                  "ITALY - AGIRA - Outlet"  328199.44  245394.82   82804.62     2621     "02 - Property Outlet"     OPEN Close
48 3145981                  "ITALY - AGIRA - Outlet" 1038007.47  726252.71  311754.76     7990     "02 - Property Outlet"     OPEN  OPEN
49 3253865     "ITALY - ORIO AL SERIO - Orio Center"  101372.27   49419.10   51953.17      685 "01 - Property Full Price"     OPEN Close
50 3253865     "ITALY - ORIO AL SERIO - Orio Center"  300679.83  121053.01  179626.82     1888 "01 - Property Full Price"     OPEN Close
51 3694385         "ITALY - ROMA - Fiumicino E DUTY" 1016438.49  366506.74  649931.75     4878 "01 - Property Full Price"     OPEN Close
52 3694385         "ITALY - ROMA - Fiumicino E DUTY" 2515591.63  884320.01 1631271.62    11906 "01 - Property Full Price"     OPEN  OPEN
53 4063154             "ITALY - MARCIANISE - Outlet"  527541.14  396824.40  130716.74     4745     "02 - Property Outlet"     OPEN Close
54 4063154             "ITALY - MARCIANISE - Outlet" 1322757.23  955939.72  366817.51    11488     "02 - Property Outlet"     OPEN  OPEN
55 4064196 "ITALY - ROMA - CASTELROMANO  Outlet NEW" 1172755.56  840966.22  331789.34     9014     "02 - Property Outlet"     OPEN  OPEN
56 4064196 "ITALY - ROMA - CASTELROMANO  Outlet NEW" 2990768.36 2061965.94  928802.42    21778     "02 - Property Outlet"     OPEN  OPEN
57 4064198 "ITALY - SERRAVALLE SCRIVIA - Outlet NEW" 2042843.11 1463925.13  578917.98    12415     "02 - Property Outlet"     OPEN  OPEN
58 4064198 "ITALY - SERRAVALLE SCRIVIA - Outlet NEW" 4812635.15 3224388.27 1588246.88    28783     "02 - Property Outlet"     OPEN  OPEN
59 4596364       "ITALY - MILANO - Buenos Aires NEW"  316817.30  142851.36  173965.94     1795 "01 - Property Full Price"     OPEN Close
60 4596364       "ITALY - MILANO - Buenos Aires NEW"  935617.88  346216.39  589401.49     5300 "01 - Property Full Price"     OPEN Close
61 4761866                  "EUR -  - ECommerce App"   56131.37   34340.30   21791.07      394           "09 - Ecommerce"     OPEN Close